/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PPrincipal.java
 *
 * Created on 20/04/2009, 11:28:56 PM
 */
package quantum;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author alumno
 */
public class PPrincipal extends javax.swing.JFrame {

    Point mouseEstoy;
    Point puntoPEstoy;
    String potencia;
    float visibleHor;
    float visibleVer;
    float unidad;
    int iniVer;
    int iniHor;
    int numZoom;
    int aMover;
    int cargaSelect;

    float dashTipo1[] = {5f, 3f};

    Timer timerDibuja = new Timer(1000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            pintaFondo();
        }
    });
    Color colores[] = {
        Color.red, Color.yellow, Color.blue,
        Color.green, Color.pink, Color.orange,
        Color.white, Color.magenta, Color.cyan
    };

    Thread hilo = new Thread(){
        @Override
        public void run(){
            timerDibuja.start();
        }
    };

    /** Creates new form PPrincipal */
    public PPrincipal() {
        iniVer = -190;
        iniHor = -215;
        visibleVer = 380;
        visibleHor = 430;
        numZoom = 0;
        cargaSelect = -1;
        potencia = "m";
        puntoPEstoy = new Point();
        initComponents();
        sldHor.setMaximum(abs(Main.plan.maxHor) + abs(Main.plan.minHor));
        sldVer.setMaximum(abs(Main.plan.maxVer) + abs(Main.plan.minHor));
        sldHor.setValue(sldHor.getMaximum() / 2);
        sldVer.setValue(sldVer.getMaximum() / 2);
        sacaUnidad();        
        hilo.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jPanel6 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        sldMov = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        labCent = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCargas = new javax.swing.JTable();
        btnBorrar = new javax.swing.JButton();
        btnAgregar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        sldVer = new javax.swing.JSlider();
        jPanel5 = new javax.swing.JPanel();
        sldHor = new javax.swing.JSlider();
        pnlBorde = new javax.swing.JPanel();
        pnlMapa = new javax.swing.JPanel();
        jButton5 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtXP = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtYP = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu6 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        chkDeca = new javax.swing.JCheckBoxMenuItem();
        chkMetros = new javax.swing.JCheckBoxMenuItem();
        chkDeci = new javax.swing.JCheckBoxMenuItem();
        chkCenti = new javax.swing.JCheckBoxMenuItem();
        chkMili = new javax.swing.JCheckBoxMenuItem();
        chkMicro = new javax.swing.JCheckBoxMenuItem();
        jMenu5 = new javax.swing.JMenu();
        chkCuad = new javax.swing.JCheckBoxMenuItem();
        chkEjes = new javax.swing.JCheckBoxMenuItem();
        chkComponentes = new javax.swing.JCheckBoxMenuItem();
        jMenu3 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Quantum 2.0v");
        setResizable(false);

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Movimiento"));

        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jPanel7.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel7.setPreferredSize(new java.awt.Dimension(40, 384));

        sldMov.setMinimum(1);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("--");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("+");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sldMov, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
            .addComponent(sldMov, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
        );

        labCent.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labCent.setText("(0 , 0)");
        labCent.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel6Layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel6Layout.createSequentialGroup()
                                        .addGap(13, 13, 13)
                                        .addComponent(labCent, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel6Layout.createSequentialGroup()
                                        .addGap(59, 59, 59)
                                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(9, 9, 9))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(59, 59, 59)))
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(6, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labCent, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Cargas"));

        jScrollPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tblCargas.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tblCargas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "No.", "X", "Y", "Carga"
            }
        ));
        tblCargas.setGridColor(new java.awt.Color(204, 51, 0));
        tblCargas.setSelectionBackground(new java.awt.Color(255, 153, 0));
        tblCargas.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblCargas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCargasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblCargas);

        btnBorrar.setText("-");
        btnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrarActionPerformed(evt);
            }
        });

        btnAgregar.setText("+");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnBorrar, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAgregar, btnBorrar});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBorrar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Visualizador"));

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel4.setPreferredSize(new java.awt.Dimension(40, 384));

        sldVer.setMaximum(1000);
        sldVer.setOrientation(javax.swing.JSlider.VERTICAL);
        sldVer.setValue(500);
        sldVer.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldVerStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(sldVer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(sldVer, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel5.setPreferredSize(new java.awt.Dimension(544, 40));

        sldHor.setMaximum(1000);
        sldHor.setValue(500);
        sldHor.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldHorStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(sldHor, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(sldHor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pnlBorde.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        pnlMapa.setBackground(new java.awt.Color(255, 255, 255));
        pnlMapa.setPreferredSize(new java.awt.Dimension(430, 380));
        pnlMapa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pnlMapaMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                pnlMapaMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                pnlMapaMouseReleased(evt);
            }
        });
        pnlMapa.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                pnlMapaMouseDragged(evt);
            }
        });

        javax.swing.GroupLayout pnlMapaLayout = new javax.swing.GroupLayout(pnlMapa);
        pnlMapa.setLayout(pnlMapaLayout);
        pnlMapaLayout.setHorizontalGroup(
            pnlMapaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 430, Short.MAX_VALUE)
        );
        pnlMapaLayout.setVerticalGroup(
            pnlMapaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 380, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout pnlBordeLayout = new javax.swing.GroupLayout(pnlBorde);
        pnlBorde.setLayout(pnlBordeLayout);
        pnlBordeLayout.setHorizontalGroup(
            pnlBordeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlMapa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        pnlBordeLayout.setVerticalGroup(
            pnlBordeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlMapa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton5, 0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
                    .addComponent(pnlBorde, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlBorde, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Punto P"));

        jLabel3.setText("X:");

        txtXP.setText(" ");
        txtXP.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtXPKeyReleased(evt);
            }
        });

        jLabel4.setText("Y:");

        txtYP.setText(" ");
        txtYP.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtYPKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtXP, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtYP, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(txtXP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(txtYP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel4))
        );

        jMenu1.setText("Archivo");
        jMenu1.addMenuListener(new javax.swing.event.MenuListener() {
            public void menuCanceled(javax.swing.event.MenuEvent evt) {
            }
            public void menuDeselected(javax.swing.event.MenuEvent evt) {
                jMenu1MenuDeselected(evt);
            }
            public void menuSelected(javax.swing.event.MenuEvent evt) {
                jMenu1MenuSelected(evt);
            }
        });

        jMenu6.setText("Resolver Prob. Resnick");

        jMenuItem4.setText("No 54 (b)");
        jMenuItem4.setPreferredSize(new java.awt.Dimension(60, 19));
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem4);

        jMenuItem3.setText("No 54 (c)");
        jMenu6.add(jMenuItem3);

        jMenuItem6.setText("No 55 (a)");
        jMenu6.add(jMenuItem6);

        jMenu1.add(jMenu6);

        jMenuItem5.setText("Salir");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Editar");
        jMenu2.addMenuListener(new javax.swing.event.MenuListener() {
            public void menuCanceled(javax.swing.event.MenuEvent evt) {
            }
            public void menuDeselected(javax.swing.event.MenuEvent evt) {
                jMenu2MenuDeselected(evt);
            }
            public void menuSelected(javax.swing.event.MenuEvent evt) {
                jMenu2MenuSelected(evt);
            }
        });
        jMenu2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu2ActionPerformed(evt);
            }
        });

        jMenuItem1.setText("Borrar P");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuItem2.setText("Posicionar P");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenu4.setText("Escala");

        chkDeca.setText("Decametros");
        chkDeca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkDecaActionPerformed(evt);
            }
        });
        jMenu4.add(chkDeca);

        chkMetros.setSelected(true);
        chkMetros.setText("Metros");
        chkMetros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkMetrosActionPerformed(evt);
            }
        });
        jMenu4.add(chkMetros);

        chkDeci.setText("Decimetros");
        chkDeci.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkDeciActionPerformed(evt);
            }
        });
        jMenu4.add(chkDeci);

        chkCenti.setText("Centimetros");
        chkCenti.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkCentiActionPerformed(evt);
            }
        });
        jMenu4.add(chkCenti);

        chkMili.setText("Milimetros");
        chkMili.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkMiliActionPerformed(evt);
            }
        });
        jMenu4.add(chkMili);

        chkMicro.setText("Micrometros");
        chkMicro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkMicroActionPerformed(evt);
            }
        });
        jMenu4.add(chkMicro);

        jMenu2.add(jMenu4);

        jMenuBar1.add(jMenu2);

        jMenu5.setText("Ver");
        jMenu5.addMenuListener(new javax.swing.event.MenuListener() {
            public void menuCanceled(javax.swing.event.MenuEvent evt) {
            }
            public void menuDeselected(javax.swing.event.MenuEvent evt) {
                jMenu5MenuDeselected(evt);
            }
            public void menuSelected(javax.swing.event.MenuEvent evt) {
                jMenu5MenuSelected(evt);
            }
        });

        chkCuad.setSelected(true);
        chkCuad.setText("Mostrar Cuadricula");
        jMenu5.add(chkCuad);

        chkEjes.setSelected(true);
        chkEjes.setText("Mostrar Ejes");
        jMenu5.add(chkEjes);

        chkComponentes.setText("Mostrar Componentes de E");
        chkComponentes.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                chkComponentesStateChanged(evt);
            }
        });
        jMenu5.add(chkComponentes);

        jMenuBar1.add(jMenu5);

        jMenu3.setText("Ayuda");
        jMenu3.addMenuListener(new javax.swing.event.MenuListener() {
            public void menuCanceled(javax.swing.event.MenuEvent evt) {
            }
            public void menuDeselected(javax.swing.event.MenuEvent evt) {
                jMenu3MenuDeselected(evt);
            }
            public void menuSelected(javax.swing.event.MenuEvent evt) {
                jMenu3MenuSelected(evt);
            }
        });
        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, 0, 241, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrarActionPerformed
        if (cargaSelect != -1) {
            Main.plan.eliminaCarga(cargaSelect);
            pintaFondo();
            actualizaTabla();            
            if(Main.plan.puntoP.ocp)
                Main.plan.calculaCampoResultante();
            cargaSelect = -1;
        }
}//GEN-LAST:event_btnBorrarActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        new AgregarCarga(this, true).setVisible(true);
        if(Main.plan.puntoP.ocp)
            Main.plan.calculaCampoResultante();
}//GEN-LAST:event_btnAgregarActionPerformed

    private void sldHorStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldHorStateChanged
        iniHor = sldHor.getValue() - abs(Main.plan.maxHor) - (int)(visibleHor / 2);
        cambiaCentro();
        pintaFondo();
}//GEN-LAST:event_sldHorStateChanged

    private void sldVerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldVerStateChanged
        iniVer = sldVer.getMaximum() - sldVer.getValue() - Main.plan.maxVer - (int)(visibleVer / 2);
        cambiaCentro();
        pintaFondo();
}//GEN-LAST:event_sldVerStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        iniHor -= sldMov.getValue();
        if (iniHor < Main.plan.minHor - (int)(visibleHor / 2)) {
            iniHor = Main.plan.minHor - (int)(visibleHor / 2);
        }
        cambiaCentro();
        pintaFondo();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        iniVer += sldMov.getValue();
        if (iniVer > Main.plan.maxVer - (int)(visibleVer / 2)) {
            iniVer = Main.plan.maxVer - (int)(visibleVer / 2);
        }
        cambiaCentro();
        pintaFondo();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        iniHor += sldMov.getValue();
        if (iniHor > Main.plan.maxHor - (int)(visibleHor / 2)) {
            iniHor = Main.plan.maxHor - (int)(visibleHor / 2);
        }
        cambiaCentro();
        pintaFondo();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        iniVer -= sldMov.getValue();
        if (iniVer < Main.plan.minVer - (int)(visibleVer / 2)) {
            iniVer = Main.plan.minVer - (int)(visibleVer / 2);
        }
        cambiaCentro();
        pintaFondo();
}//GEN-LAST:event_jButton4ActionPerformed

    private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu2ActionPerformed
        pintaFondo();
    }//GEN-LAST:event_jMenu2ActionPerformed

    private void tblCargasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCargasMouseClicked
        cargaSelect = tblCargas.getSelectedRow();
        System.out.println(cargaSelect);
    }//GEN-LAST:event_tblCargasMouseClicked

    private void jMenu1MenuSelected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_jMenu1MenuSelected
        timerDibuja.stop();
    }//GEN-LAST:event_jMenu1MenuSelected

    private void jMenu2MenuSelected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_jMenu2MenuSelected
        timerDibuja.stop();
    }//GEN-LAST:event_jMenu2MenuSelected

    private void jMenu3MenuSelected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_jMenu3MenuSelected
        timerDibuja.stop();
    }//GEN-LAST:event_jMenu3MenuSelected

    private void jMenu1MenuDeselected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_jMenu1MenuDeselected
        timerDibuja.start();
    }//GEN-LAST:event_jMenu1MenuDeselected

    private void jMenu2MenuDeselected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_jMenu2MenuDeselected
        timerDibuja.start();
    }//GEN-LAST:event_jMenu2MenuDeselected

    private void jMenu3MenuDeselected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_jMenu3MenuDeselected
        timerDibuja.start();
    }//GEN-LAST:event_jMenu3MenuDeselected

    private void pnlMapaMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlMapaMouseDragged
        if(aMover == 0){
            muevePantalla(evt);
        }else{
            muevePunto(evt);
        }
    }//GEN-LAST:event_pnlMapaMouseDragged

    private void pnlMapaMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlMapaMousePressed
         mouseEstoy = evt.getPoint();
         timerDibuja.stop();
         if(noPuntoP()){
            aMover = 1;
         }else{
            aMover = 0;
         }
    }//GEN-LAST:event_pnlMapaMousePressed

    private void pnlMapaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlMapaMouseReleased
        timerDibuja.start();
    }//GEN-LAST:event_pnlMapaMouseReleased

    private void pnlMapaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlMapaMouseClicked
        int auxX = evt.getX();
        int auxY = evt.getY();
        int medX = (int) (visibleHor / 2);
        int medY = (int) (visibleVer / 2);
        iniHor += auxX - medX;
        iniVer += auxY - medY;
        pintaFondo();
        cambiaCentro();
    }//GEN-LAST:event_pnlMapaMouseClicked

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        Main.control.setVisible(true);
        Main.control.actualiza();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        new AgregaPunto(this, true).setVisible(true);
        Main.plan.calculaCampoResultante();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        Main.plan.puntoP.ocp = false;
        txtXP.setText("");
        txtYP.setText("");
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void chkMetrosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkMetrosActionPerformed
        borraEscala();
        chkMetros.setState(true);
        numZoom = 0;
        potencia = "m";
        actualizaTabla();
        sacaUnidad();
        cambiaCentroPunto();
        Main.control.actualiza();
}//GEN-LAST:event_chkMetrosActionPerformed

    private void jMenu5MenuSelected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_jMenu5MenuSelected
        timerDibuja.stop();
    }//GEN-LAST:event_jMenu5MenuSelected

    private void jMenu5MenuDeselected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_jMenu5MenuDeselected
        timerDibuja.start();
    }//GEN-LAST:event_jMenu5MenuDeselected

    private void chkDecaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkDecaActionPerformed
        borraEscala();
        chkDeca.setState(true);
        numZoom = 1;
        potencia = "x10^1 m";
        actualizaTabla();
        sacaUnidad();
        cambiaCentroPunto();
        Main.control.actualiza();
    }//GEN-LAST:event_chkDecaActionPerformed

    private void chkDeciActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkDeciActionPerformed
        borraEscala();
        chkDeci.setState(true);
        numZoom = -1;
        potencia = "x10^-1 m";
        actualizaTabla();
        sacaUnidad();
        cambiaCentroPunto();
        Main.control.actualiza();
    }//GEN-LAST:event_chkDeciActionPerformed

    private void chkCentiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkCentiActionPerformed
        borraEscala();
        chkCenti.setState(true);
        numZoom = -2;
        potencia = "x10^-2 m";
        actualizaTabla();
        sacaUnidad();
        cambiaCentroPunto();
        Main.control.actualiza();
    }//GEN-LAST:event_chkCentiActionPerformed

    private void chkMiliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkMiliActionPerformed
        borraEscala();
        chkMili.setState(true);
        numZoom = -3;
        potencia = "x10^-3 m";
        actualizaTabla();
        sacaUnidad();
        cambiaCentroPunto();
        Main.control.actualiza();
    }//GEN-LAST:event_chkMiliActionPerformed

    private void chkMicroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkMicroActionPerformed
        borraEscala();
        chkMicro.setState(true);
        numZoom = -6;
        potencia = "x10^-6 m";
        sacaUnidad();
        actualizaTabla();
        cambiaCentroPunto();
        Main.control.actualiza();
    }//GEN-LAST:event_chkMicroActionPerformed

    private void txtXPKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtXPKeyReleased
        float aux;
        if(txtXP.getText().length() != 0){
            aux = Float.parseFloat(txtXP.getText());
            if(numZoom > 0){  //Entonces hay que dividir
                aux /= (pot(numZoom));
            }else{
                aux *= (pot(numZoom*-1));
            }
            Main.plan.puntoP.X = aux;
            Main.plan.calculaCampoResultante();
            Main.control.actualiza();
        }else{
            Main.plan.puntoP.X = 0f;
            Main.plan.calculaCampoResultante();
            Main.control.actualiza();
        }
    }//GEN-LAST:event_txtXPKeyReleased

    private void txtYPKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtYPKeyReleased
        float aux;
        if(txtYP.getText().length() != 0){
            aux = Float.parseFloat(txtYP.getText());
            if(numZoom > 0){  //Entonces hay que dividir
                aux /= (pot(numZoom));
            }else{
                aux *= (pot(numZoom*-1));
            }
            Main.plan.puntoP.Y = -aux;
            Main.plan.calculaCampoResultante();
            Main.control.actualiza();
        }else{
            Main.plan.puntoP.Y = 0f;
            Main.plan.calculaCampoResultante();
            Main.control.actualiza();
        }
    }//GEN-LAST:event_txtYPKeyReleased

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void chkComponentesStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_chkComponentesStateChanged
        Main.control.btnComponentes.setSelected(chkComponentes.getState());
        if(Main.prin.chkEjes.getState() == false && chkComponentes.getState() == true){
            Main.prin.chkEjes.setState(true);
        }
    }//GEN-LAST:event_chkComponentesStateChanged

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        Carga dato = new Carga(-0.03f, 0f, 0.000000006f, Color.red);
        Main.plan.anadeCarga(dato);
        dato = new Carga(0.03f, 0f, 0.000000003f, Color.red);
        Main.plan.anadeCarga(dato);        
        borraEscala();
        chkMili.setState(true);
        numZoom = -3;
        potencia = "x10^-3 m";
        actualizaTabla();
        sacaUnidad();
        cambiaCentroPunto();
        Main.control.actualiza();
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    void sacaUnidad(){
        if(numZoom > 0){  //Entonces hay que dividir
            unidad = 1f * (float)(pot(numZoom));
        }else{
            unidad = 1f / (float)(pot(numZoom * -1));
        }
        System.out.println(unidad);
    }

    void muevePunto(java.awt.event.MouseEvent evt){
        Main.plan.puntoP.X -= (mouseEstoy.x - evt.getX()) * unidad;
        Main.plan.puntoP.Y -= (mouseEstoy.y - evt.getY()) * unidad;
        if (Main.plan.puntoP.X < (Main.plan.minHor - (int)(visibleHor / 2))) {
            Main.plan.puntoP.X = (Main.plan.minHor - (int)(visibleHor / 2));
        }
        if (Main.plan.puntoP.X > (Main.plan.maxHor - (int)(visibleHor / 2))) {
            Main.plan.puntoP.X = (Main.plan.maxHor - (int)(visibleHor / 2));
        }
        if (Main.plan.puntoP.X < (Main.plan.minVer - (int)(visibleVer / 2))){
            Main.plan.puntoP.X = (Main.plan.minVer - (int)(visibleVer / 2));
        }
        if (Main.plan.puntoP.X > (Main.plan.maxVer - (int)(visibleVer / 2))) {
            Main.plan.puntoP.X = (Main.plan.maxVer - (int)(visibleVer / 2));
        }
        mouseEstoy = evt.getPoint();
        Main.plan.calculaCampoResultante();
        Main.control.actualiza();
        pintaFondo();
        cambiaCentroPunto();
    }

    void muevePantalla(java.awt.event.MouseEvent evt){
        Point aux = new Point();
        aux.x = mouseEstoy.x - evt.getX();
        aux.y = mouseEstoy.y - evt.getY();
        iniHor += aux.x;
        iniVer += aux.y;
        if (iniHor < (Main.plan.minHor - (int)(visibleHor / 2))) {
            iniHor = (Main.plan.minHor - (int)(visibleHor / 2));
        }
        if (iniHor > (Main.plan.maxHor - (int)(visibleHor / 2))) {
            iniHor = (Main.plan.maxHor - (int)(visibleHor / 2));
        }
        if (iniVer < (Main.plan.minVer - (int)(visibleVer / 2))){
            iniVer = (Main.plan.minVer - (int)(visibleVer / 2));
        }
        if (iniVer > (Main.plan.maxVer - (int)(visibleVer / 2))) {
            iniVer = (Main.plan.maxVer - (int)(visibleVer / 2));
        }
        mouseEstoy = evt.getPoint();
        pintaFondo();
        cambiaCentro();
    }

    boolean noPuntoP(){
        boolean reg = false;
        if(mouseEstoy.x <= puntoPEstoy.x + 5 && mouseEstoy.x >= puntoPEstoy.x - 5){
            if(mouseEstoy.y <= puntoPEstoy.y + 5 && mouseEstoy.y >= puntoPEstoy.y - 5){
                reg = true;
            }
        }
        return reg;
    }

    void borraEscala(){
        switch(numZoom){
            case 1:
                chkDeca.setState(false);
                break;
            case 0:
                chkMetros.setState(false);
                break;
            case -1:
                chkDeci.setState(false);
                break;
            case -2:
                chkCenti.setState(false);
                break;
            case -3:
                chkMili.setState(false);
                break;
            case -6:
                chkMicro.setState(false);
                break;
        }
    }

    void cambiaCentro() {
        int aux;
        String val;
        sldVer.setValue(sldVer.getMaximum() - (iniVer - Main.plan.minVer + (int)(visibleVer / 2)));
        sldHor.setValue(iniHor + Main.plan.maxHor + (int)(visibleHor / 2));
        val = "(";
        val += (iniHor + (visibleHor / 2));
        val += " , ";
        val += (iniVer + (visibleVer / 2)) * -1;
        val += ")";
        labCent.setText(val);
    }

    void cambiaCentroPunto(){
        float auxX;
        float auxY;
        if(numZoom > 0){  //Entonces hay que dividir
            auxX = Main.plan.puntoP.X / (pot(numZoom));
            auxY = Main.plan.puntoP.Y / (pot(numZoom));
        }else{
            auxX = Main.plan.puntoP.X * (pot(numZoom*-1));
            auxY = Main.plan.puntoP.Y * (pot(numZoom*-1));
        }
        txtXP.setText(String.valueOf(Math.floor(auxX * 100)/100));
        txtYP.setText(String.valueOf(Math.floor(auxY * 100)/100));
    }

    void actualizaTabla() {
        Object[] obj = new Object[5];
        float aux;
        try {
            DefaultTableModel model = new DefaultTableModel(new Object[][]{},
                    new String[]{
                        "No.", "X", "Y", "Carga"
                    }) {

                Class[] types = new Class[]{
                    java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
                };
                boolean[] canEdit = new boolean[]{
                    false, false, false, false
                };

                public Class getColumnClass(int columnIndex) {
                    return types[columnIndex];
                }

                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit[columnIndex];
                }
            };
            for (int i = 1; i <= Main.plan.cargas.nCargas; i++) {
                if (Main.plan.cargas.cargas[i].ocp) {
                    obj[0] = i + 1;
                    if(numZoom > 0){
                        aux = Main.plan.cargas.cargas[i].X / pot(numZoom);
                    }else{
                        aux = Main.plan.cargas.cargas[i].X * pot(numZoom * -1);
                    }
                    obj[1] = aux + potencia;
                    if(numZoom > 0){
                        aux = Main.plan.cargas.cargas[i].Y / pot(numZoom);
                    }else{
                        aux = Main.plan.cargas.cargas[i].Y * pot(numZoom * -1);
                    }
                    aux *= -1;
                    obj[2] = aux + potencia;
                    obj[3] = Main.plan.cargas.cargas[i].magnitud + "C";                    
                    model.addRow(obj);
                }
            }
            tblCargas.setModel(model);
            tblCargas.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error, al intentar procesas los puntos.", "Alerta", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }

    void dibujaEjes(){
        Graphics g = pnlMapa.getGraphics();
        Dimension dim = pnlMapa.getSize();
        //Se pintan los Ejes
        for (int y = iniVer; y <= dim.height + visibleVer; y++) {
            if (y == -1) {
                g.setColor(Color.black);
                g.fillRect(0, y - iniVer, dim.width, 3);
            //g.drawLine(0, y - iniVer, dim.width, y - iniVer);
            }
        }
        for (int x = iniHor; x <= dim.width + visibleHor; x++) {
            if (x == -1) {
                g.setColor(Color.black);
                g.fillRect(x - iniHor, 0, 3, dim.height);
            //g.drawLine(x - iniHor, 0, x - iniHor, dim.height);
            }
        }
    }

    int abs(int a){
        if(a < 0)
            return -a;
        return a;
    }

    int pot(int cuantos){
        int reg = 1;
        for(int i = 1; i <= cuantos; i ++){
            reg *= 10;
        }
        return reg;
    }

    void dibujaPuntos(){
        Graphics g = pnlMapa.getGraphics();
        Graphics2D g2d = (Graphics2D)(g);
        float res;
        float magEx;
        float magEy;
        int auxX;
        int auxY;
        int auxEsc;
        // Aqui se pintan los puntos        
        for (int i = 1; i <= Main.plan.cargas.nCargas; i++){
            //Aqui debe de calcular el escalado, para X
            if(numZoom > 0){  //Entonces hay que dividir
                res = Main.plan.cargas.cargas[i].X / (pot(numZoom));
                auxX = (int)(res);
                res = Main.plan.cargas.cargas[i].Y / (pot(numZoom));
                auxY = (int)(res);
            }else{                                
                res = Main.plan.cargas.cargas[i].X * (pot(numZoom*-1));
                auxX = (int)(res);
                res = Main.plan.cargas.cargas[i].Y * (pot(numZoom*-1));
                auxY = (int)(res);
            }                        
            if (auxX >= iniHor + 10 && auxX <= iniHor + visibleHor - 10) {
                if (auxY >= iniVer + 10 && auxY <= iniVer + visibleVer - 10) {
                    g.setColor(Main.plan.cargas.cargas[i].color);
                    g.fillOval(auxX - iniHor - 5, auxY - iniVer - 5, 10, 10);
                    g.setColor(Color.black);
                    g.drawOval(auxX - iniHor - 5, auxY - iniVer - 5, 10, 10);                    
                }
            }
        }
        //Dibuja el punto P
        if(Main.plan.puntoP.ocp){
            //Aqui debe de calcular el escalado, para X
            if(numZoom > 0){  //Entonces hay que dividir
                res = Main.plan.puntoP.X / (pot(numZoom));
                auxX = (int)(res);
                res = Main.plan.puntoP.Y / (pot(numZoom));
                auxY = (int)(res);
                magEx = Main.plan.campoElcRes.X / (pot(numZoom)) *-1;
                magEy = Main.plan.campoElcRes.Y / (pot(numZoom));                
            }else{
                res = Main.plan.puntoP.X * (pot(numZoom * -1));
                auxX = (int)(res);
                res = Main.plan.puntoP.Y * (pot(numZoom * -1));
                auxY = (int)(res);
                magEx = Main.plan.campoElcRes.X * (pot(numZoom * -1)) * -1;
                magEy = Main.plan.campoElcRes.Y * (pot(numZoom * -1));                
            }
            g.setColor(Main.plan.puntoP.color);
            g.fillOval(auxX - iniHor - 1, auxY - iniVer - 1, 3, 3);
            g2d.setStroke(new BasicStroke(3.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
            g2d.setPaint(Main.plan.puntoP.color);
            Line2D line = new Line2D.Double((double)(auxX - iniHor), (double)(auxY - iniVer), (double)(auxX - iniHor - magEx), (double)(auxY - iniVer + magEy));            
            g2d.draw(line);
            //dibuja la flecha del vector            
            g2d.setColor(Color.blue);
            if(numZoom > 0){  //Entonces hay que dividir
                Vector2 vUno = Vector2.calculaFlecha(Main.plan.campoElcRes, 8.0f * pot(numZoom), Math.tan(Math.toRadians(-Main.plan.angEx + 30)));
                Vector2 vDos = Vector2.calculaFlecha(Main.plan.campoElcRes, 8.0f * pot(numZoom), Math.tan(Math.toRadians(-Main.plan.angEx - 30)));
                Line2D lineaFlechaUno = new Line2D.Double(vUno.Xi / pot(numZoom) - iniHor, vUno.Yi / pot(numZoom) - iniVer, (vUno.Xi + vUno.X) / pot(numZoom) - iniHor, (vUno.Yi+ vUno.Y) / pot(numZoom) - iniVer);
                Line2D lineaFlechaDos = new Line2D.Double(vDos.Xi / pot(numZoom) - iniHor, vDos.Yi / pot(numZoom) - iniVer, (vDos.Xi + vDos.X) / pot(numZoom) - iniHor, (vDos.Yi+ vDos.Y) / pot(numZoom) - iniVer);
                g2d.draw(lineaFlechaUno);
                g2d.draw(lineaFlechaDos);
            }else{
                Vector2 vUno = Vector2.calculaFlecha(Main.plan.campoElcRes, 8.0f / (pot(numZoom * -1)), Math.tan(Math.toRadians(-Main.plan.angEx + 30)));
                Vector2 vDos = Vector2.calculaFlecha(Main.plan.campoElcRes, 8.0f / (pot(numZoom * -1)), Math.tan(Math.toRadians(-Main.plan.angEx - 30)));
                Line2D lineaFlechaUno = new Line2D.Double(vUno.Xi * (pot(numZoom * -1)) - iniHor, vUno.Yi * (pot(numZoom * -1)) - iniVer, (vUno.Xi + vUno.X) * (pot(numZoom * -1)) - iniHor, (vUno.Yi+ vUno.Y) * (pot(numZoom * -1)) - iniVer);
                Line2D lineaFlechaDos = new Line2D.Double(vDos.Xi * (pot(numZoom * -1)) - iniHor, vDos.Yi * (pot(numZoom * -1)) - iniVer, (vDos.Xi + vDos.X) * (pot(numZoom * -1)) - iniHor, (vDos.Yi+ vDos.Y) * (pot(numZoom * -1))- iniVer);
                g2d.draw(lineaFlechaUno);
                g2d.draw(lineaFlechaDos);
            }
            //Dibuja las componentes
            if(chkComponentes.getState()){
                g2d.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 1f, dashTipo1, 0f));
                Line2D lineHor = new Line2D.Double(-iniHor, (double)(auxY - iniVer + magEy), (double)(auxX - iniHor - magEx), (double)(auxY - iniVer + magEy));
                Line2D lineVer = new Line2D.Double((double)(auxX - iniHor - magEx), - iniVer, (double)(auxX - iniHor - magEx), (double)(auxY - iniVer + magEy));
                g2d.setColor(Color.red);
                g2d.draw(lineHor);
                g2d.draw(lineVer);
                lineHor = new Line2D.Double(-iniHor, (double)(auxY - iniVer), (double)(auxX - iniHor), (double)(auxY - iniVer));
                lineVer = new Line2D.Double((double)(auxX - iniHor), - iniVer, (double)(auxX - iniHor), (double)(auxY - iniVer));
                g2d.draw(lineHor);
                g2d.draw(lineVer);
            }
            puntoPEstoy.x = auxX - iniHor;
            puntoPEstoy.y = auxY - iniVer;
        }
    }

    void dibujaReticula(){
        Graphics g = pnlMapa.getGraphics();
        Dimension dim = pnlMapa.getSize();
        Point aux;
        // Se pintan las lineas claras
        for (int y = iniVer; y <= dim.height + visibleVer; y++) {
            if (y % 10 == 0) {
                g.setColor(Color.lightGray);
                g.drawLine(0, y - iniVer, dim.width, y - iniVer);
            }
        }
        for (int x = iniHor; x <= dim.width + visibleHor; x++) {
            if (x % 10 == 0) {
                g.setColor(Color.lightGray);
                g.drawLine(x - iniHor, 0, x - iniHor, dim.height);
            }
        }
        //Se pintan las lineas Gruesas
        for (int y = iniVer; y <= dim.height + visibleVer; y++) {
            if (y % 100 == 0) {
                g.setColor(Color.darkGray);
                g.drawLine(0, y - iniVer, dim.width, y - iniVer);
            }
        }
        for (int x = iniHor; x <= dim.width + visibleHor; x++) {
            if (x % 100 == 0) {
                g.setColor(Color.darkGray);
                g.drawLine(x - iniHor, 0, x - iniHor, dim.height);
            }
        }
    }

    void pintaFondo() {
        Graphics g = pnlMapa.getGraphics();
        Dimension dim = pnlMapa.getSize();
        g.setColor(Color.white);
        g.fillRect(0, 0, dim.width, dim.height);
        if(chkCuad.getState()){
            dibujaReticula();
        }
        if(chkEjes.getState()){
            dibujaEjes();
        }
        dibujaPuntos();
    }

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnBorrar;
    private javax.swing.JCheckBoxMenuItem chkCenti;
    public javax.swing.JCheckBoxMenuItem chkComponentes;
    private javax.swing.JCheckBoxMenuItem chkCuad;
    private javax.swing.JCheckBoxMenuItem chkDeca;
    private javax.swing.JCheckBoxMenuItem chkDeci;
    private javax.swing.JCheckBoxMenuItem chkEjes;
    private javax.swing.JCheckBoxMenuItem chkMetros;
    private javax.swing.JCheckBoxMenuItem chkMicro;
    private javax.swing.JCheckBoxMenuItem chkMili;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel labCent;
    private javax.swing.JPanel pnlBorde;
    private javax.swing.JPanel pnlMapa;
    private javax.swing.JSlider sldHor;
    private javax.swing.JSlider sldMov;
    private javax.swing.JSlider sldVer;
    private javax.swing.JTable tblCargas;
    public javax.swing.JTextField txtXP;
    public javax.swing.JTextField txtYP;
    // End of variables declaration//GEN-END:variables
}
